package edu.sdwu.liqing.qasystem.util;

import edu.sdwu.liqing.qasystem.entity.BaseEntity;
import edu.sdwu.liqing.qasystem.entity.QaUser;
import edu.sdwu.liqing.qasystem.service.IQaModuleService;
import edu.sdwu.liqing.qasystem.service.IQaRoleModuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

//权限工具类
@Component
public class OperateUtil {

    @Autowired
    RequestUtil requestUtil;

    @Autowired
    IQaModuleService moduleService;

    @Autowired
    IQaRoleModuleService roleModuleService;

    public boolean canOperate(BaseEntity baseEntity) {
        //获取当前用户
        QaUser qaUser = requestUtil.getCurrentUser();
        if (Objects.isNull(qaUser)) {
            return false;
        }
        //然后开始判断
        if (StringUtils.containsIgnoreCase(qaUser.getRoleName(), "ADMIN")) {
            return true;
        }
        return StringUtils.equalsIgnoreCase(qaUser.getId(), baseEntity.getCreatedBy());
    }

    public boolean canAddQuestion(String moduleId) {
        //获取当前用户
        QaUser qaUser = requestUtil.getCurrentUser();
        if (Objects.isNull(qaUser)) {
            return false;
        }
        if (StringUtils.containsIgnoreCase(qaUser.getRoleName(), "ADMIN")) {
            return true;
        }
        List<String> userList = roleModuleService.getRoleIdListByModuleId(moduleId);
        if (userList.contains(qaUser.getRoleId())) {
            return true;
        }
        return false;
    }
}
